home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 146_01 / wc.c < prev   
Text File  |  1985-03-09  |  2KB  |  64 lines

  1. /*
  2. HEADER:        CUG146.07;
  3. TITLE:        Word Count;
  4. DESCRIPTION:      "This program counts total number of lines, words and
  5.                   characters in the specified file."
  6. FILENAME:         wc.c;
  7. */
  8.  
  9.               
  10. /*  This program counts total number of lines, words
  11.        and characters in the specified file           */
  12.  
  13. #define CR 13
  14. #define YES 1
  15. #define NO 0
  16.  
  17. main(argc,argv)
  18.        int argc, *argv[];
  19. {      int c, lines, nw, nc, nc1, inword, *fp;
  20.        if(argc != 2) return;
  21.        fp=fopen(argv[1],"r");
  22.        inword=NO;
  23.        lines=nw=nc=nc1=0;
  24.        nl();
  25.        while((c=getc(fp)) >= 0)
  26.        {       ++nc;
  27.                if(nc > 9999)
  28.                {       ++nc1; nc=0;
  29.                        }
  30.                if(c == CR)
  31.                        ++lines;
  32.                if(c <= ' ')
  33.                        inword=NO;
  34.                else if(inword == NO)
  35.                {       inword=YES;
  36.                        ++nw;
  37.                        }
  38.                }
  39.        nl();puts("# of lines = ");putnum(lines);
  40.        nl();puts("# of words = ");putnum(nw);
  41.        nl();puts("# of chars = ");
  42.        if(nc1)  {
  43.                putnum(nc1);puts("0000 + ");
  44.                 }
  45.        putnum(nc);
  46.        nl();
  47.        fclose(fp);
  48.        }
  49.  
  50. nl()
  51. {      putchar(CR);
  52.        }
  53.  
  54. putnum(n)
  55.        int n;
  56. {      if(n < 0)
  57.        {       putchar('-');
  58.                n=(-n);
  59.                }
  60.        if(n > 9)
  61.                putnum(n/10);
  62.        putchar('0'+(n%10));
  63.        }
  64.